library(plyr)
library(stringr)
library(magrittr)
library(tidyverse)
library(readxl)
library(gap)
library(stargazer)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


dv_floor = paste0(home, 'Data/floor-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment),
         select=c(Spoke, 
                  Citizen,
                  Prop.Citizen,
                  Firm,
                  Prop.Firm,
                  FullTime,
                  CentNom,
                  Competitive))


dv_caucus = paste0(home, 'Data/caucus-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment),
         select=c(Spoke, 
                  Citizen,
                  Prop.Citizen,
                  Firm,
                  Prop.Firm,
                  FullTime,
                  CentNom,
                  Competitive))


f1 = chow.test(y1=subset(x=dv_caucus, 
                         select=Spoke),
               x1=subset(x=dv_caucus,
                         select=-Spoke),
               y2=subset(x=dv_floor,
                         select=Spoke),
               x2=subset(x=dv_floor,
                         select=-Spoke)) %>%
  as.data.frame %>%
  set_rownames(c('F-stat.','df 1','df 2','p-value'))


f2 = chow.test(y1=subset(x=dv_caucus, 
                         select=Spoke),
               x1=subset(x=dv_caucus,
                         select=-c(Spoke, FullTime, CentNom, Competitive)),
               y2=subset(x=dv_floor,
                         select=Spoke),
               x2=subset(x=dv_floor,
                         select=-c(Spoke, FullTime, CentNom, Competitive))) %>%
  as.data.frame %>%
  set_rownames(c('F-stat.','df 1','df 2','p-value'))


f3 = chow.test(y1=subset(x=dv_caucus, 
                         select=Spoke),
               x1=subset(x=dv_caucus,
                         select=-c(Spoke, Firm, Prop.Firm)),
               y2=subset(x=dv_floor,
                         select=Spoke),
               x2=subset(x=dv_floor,
                         select=-c(Spoke, Firm, Prop.Firm))) %>%
  as.data.frame %>%
  set_rownames(c('F-stat.','df 1','df 2','p-value'))


f4 = chow.test(y1=subset(x=dv_caucus, 
                         select=Spoke),
               x1=subset(x=dv_caucus,
                         select=-c(Spoke, Firm, Prop.Firm, FullTime, CentNom, Competitive)),
               y2=subset(x=dv_floor,
                         select=Spoke),
               x2=subset(x=dv_floor,
                         select=-c(Spoke, Firm, Prop.Firm, FullTime, CentNom, Competitive))) %>%
  as.data.frame %>%
  set_rownames(c('F-stat.','df 1','df 2','p-value'))


cbind(f1, f2, f3, f4) %>% 
  t %>%
  set_rownames(c('Full interactive',
                 'Dropping controls',
                 'Dropping firm terms',
                 'Dropping controls + firm terms')) %>% 
  as.data.frame %>%
  subset(select=c(`F-stat.`,
                  `p-value`)) %>%
  mutate(`p-value`=ifelse(`p-value`<0.001, '<0.001', `p-value`)) %>%
  stargazer(summary=F,
            align=T,
            header=F,
            label='tab:chow',
            title='All Chow test specifications reveal a meaningful difference between the caucuses and floor debates.', 
            notes='Full interactive specification includes Cit., %Cit., Firm, %Firm, Fulltime, CentNom, and Competitive.',
            notes.append=F) %>%
  writeClipboard
